import TmcFooter from '@/components/Footer'
import { Trim,setStore,setSStore,getStore,removeStore } from "@/utils/util"
export default {
	data() {
		return {
			userName:"",//用户名
			passWord:"",//密码
			remember:true,//是否记住密码		
		}
	},
	computed: {
		
	},
	watch:{
		
	},
	methods: {
		//登录
		login(){	
			let data = {
				username:Trim(this.userName),
				password:Trim(this.passWord)
			}			
			//登录请求
			this.$http.post("/user/login",data)
				.then((res) => {					
					let msg = res.data.msg;
					if(msg&&msg=="success"){ //登录成功
						this.$message('登录成功');
						this.isRememberPassword(data);//记住密码操作
						setSStore('user',res.data)//将登陆者的信息存入sessionStorage
						setSStore('token',res.data.token)//将登陆者token信息存入sessionStorage
						this.$router.push({ path: '/home' });//页面跳转
						return;
					}
					this.$message.error('登录失败');
				})
		},
		
		//是否记住密码
		isRememberPassword(data){
			if(this.remember){
				setStore("loginMess",data);
				return;
			}
			if(getStore("loginMess")){
				removeStore("loginMess")
			}			
		},
		//获取用户名密码
		getLoginMess(){
			let loginMess = getStore("loginMess");
			if(loginMess){
				this.userName = loginMess.username;
				this.passWord = loginMess.password;
			}
		}
	},
	components:{
		TmcFooter,
	},
	created() {
		this.getLoginMess();
	}
}